﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Bubblez
{
    public partial class frmfilters : Form
    {
        // Allow Access to values in other form.
        private frmmain main = new frmmain();

        // Paths
        public string Filter_Path;

        // Default Filters
        public string[] Filter_Default;
        public string[] Filter_Default_Name;
        public string[] Filter_Default_String;
        public bool[] Filter_Default_Title;
        public bool[] Filter_Default_Author;
        public bool[] Filter_Default_Notes;
        public bool[] Filter_Default_HasDep;
        public int[] Filter_Default_Mode;
        public bool[] Filter_Default_Enabled;

        // External Filters
        public string[] Filter;
        public string[] Filter_Name;
        public string[] Filter_String;
        public bool[] Filter_Title;
        public bool[] Filter_Author;
        public bool[] Filter_Notes;
        public bool[] Filter_HasDep;
        public int[] Filter_Mode;
        public bool[] Filter_Enabled;

        // Enable
        public int Enable_Mode;

        // Activate?
        public bool Use_Activate = false;

        public frmfilters()
        {
            InitializeComponent();
        }

        private void frmfilters_Load(object sender, EventArgs e)
        {
            // Load Paths
            Filter_Path = main.Filter_Path;

            // Initialize Default Filter Arrays
            Filter_Default = new string[main.Filter_Default.Length];
            Filter_Default_Name = new string[main.Filter_Default_Name.Length];
            Filter_Default_String = new string[main.Filter_Default.Length];
            Filter_Default_Title = new bool [main.Filter_Default.Length];
            Filter_Default_Author = new bool [main.Filter_Default.Length];
            Filter_Default_Notes = new bool [main.Filter_Default.Length];
            Filter_Default_HasDep = new bool [main.Filter_Default.Length];
            Filter_Default_Mode = new int [main.Filter_Default.Length];
            Filter_Default_Enabled = new bool [main.Filter_Default.Length];

            // Import Default Filters
            for (int i = 0; i <= main.Filter_Default.Length - 1; i++)
            {
                Filter_Default[i] = main.Filter_Default[i];
                Filter_Default_Name[i] = main.Filter_Default_Name[i];
                Filter_Default_String[i] = main.Filter_Default_String[i];
                Filter_Default_Title[i] = main.Filter_Default_Title[i];
                Filter_Default_Author[i] = main.Filter_Default_Author[i];
                Filter_Default_Notes[i] = main.Filter_Default_Notes[i];
                Filter_Default_HasDep[i] = main.Filter_Default_HasDep[i];
                Filter_Default_Mode[i] = main.Filter_Default_Mode[i];
                Filter_Default_Enabled[i] = main.Filter_Default_Enabled[i];
            }

            string[] f = Directory.GetFiles(Application.StartupPath + "\\Filters");
            if (f.Length > 0)
            {
                Filter = new string[f.Length];
                Filter_Name = new string[f.Length];
                Filter_String = new string[f.Length];
                Filter_Title = new bool[f.Length];
                Filter_Author = new bool[f.Length];
                Filter_Notes = new bool[f.Length];
                Filter_HasDep = new bool[f.Length];
                Filter_Mode = new int[f.Length];
                Filter_Enabled = new bool[f.Length];

                Filter = Directory.GetFiles(Application.StartupPath + "\\Filters");
                for (int i = 0; i <= f.Length - 1; i++)
                    Filter_LoadFile(Filter[i], i);
            }

            Disable_All();
        }
        private void Filter_LoadFile(string path, int index)
        {
            if (File.Exists(path) == false)
            {
                Filter_String[index] = "Error: File Not Found (" + path + ")";
                Filter_Title[index] = false;
                Filter_Author[index] = false;
                Filter_Notes[index] = false;
                Filter_HasDep[index] = false;
                Filter_Enabled[index] = false;
            }
            string[] line = File.ReadAllLines(path);
            for (int i = 0; i <= line.Length - 1; i++)
            {
                if ((line[i].Substring(0, 2)).ToLower() == "na")
                    Filter_Name[index] = line[i].Substring(5, line[i].Length - 5);
                else if ((line[i].Substring(0, 2)).ToLower() == "st")
                    Filter_String[index] = line[i].Substring(7, line[i].Length - 7);
                else if ((line[i].Substring(0, 2)).ToLower() == "ti")
                    Filter_Title[index] = Convert.ToBoolean(line[i].Substring(6, line[i].Length - 6));
                else if ((line[i].Substring(0, 2)).ToLower() == "au")
                    Filter_Author[index] = Convert.ToBoolean(line[i].Substring(7, line[i].Length - 7));
                else if ((line[i].Substring(0, 2)).ToLower() == "no")
                    Filter_Notes[index] = Convert.ToBoolean(line[i].Substring(6, line[i].Length - 6));
                else if ((line[i].Substring(0, 2)).ToLower() == "ha")
                    Filter_HasDep[index] = Convert.ToBoolean(line[i].Substring(7, line[i].Length - 7));
                else if ((line[i].Substring(0, 2)).ToLower() == "mo")
                    Filter_Mode[index] = Convert.ToInt16(line[i].Substring(5, line[i].Length - 5));
                else if ((line[i].Substring(0, 2)).ToLower() == "en")
                    Filter_Enabled[index] = Convert.ToBoolean(line[i].Substring(8, line[i].Length - 8));

            }
        }
        private void Filter_Save()
        {
            for (int i = 0; i < Filter.Length; i++)
            {
                string[] l = new string[] {"Name=" + Filter_Name[i], "String=" + Filter_String[i], "Title=" + Convert.ToString(Filter_Title[i]), "Author=" + Convert.ToString(Filter_Title[i]), "Notes=" + Convert.ToString(Filter_Notes[i]), "HasDep=" + Convert.ToString(Filter_HasDep[i]), "Mode=" + Filter_Mode[i], "Enabled=" + Convert.ToString(Filter_Enabled[i])};
                File.WriteAllLines(Filter_Path + "\\" + Filter_Name[i] + ".filter", l);
            }
        }

        private int Get_Mode()
        {
            if (rdoExact.Checked == true)
                return 1;
            else if (rdoMatch.Checked == true)
                return 2;
            else if (rdoContains.Checked == true)
                return 3;
            else
                return 0;
        }
        private bool Get_eState()
        {
            if (cmdEnable.Text == "Enable")
                return false;
            else
                return true;
        }
        private void Enable_All()
        {
            chkAuthor.Enabled = true;
            chkdep.Enabled = true;
            chkNotes.Enabled = true;
            chkTitle.Enabled = true;

            rdoContains.Enabled = true;
            rdoExact.Enabled = true;
            rdoMatch.Enabled = true;

            txtString.Enabled = true;

            cmdDelete.Enabled = true;
            cmdNew.Enabled = true;
            cmdEnable.Enabled = true;
        }
        private void Disable_All()
        {
            chkAuthor.Enabled = false;
            chkdep.Enabled = false;
            chkNotes.Enabled = false;
            chkTitle.Enabled = false;

            rdoContains.Enabled = false;
            rdoExact.Enabled = false;
            rdoMatch.Enabled = false;

            txtString.Enabled = false;

            cmdDelete.Enabled = false;
            cmdNew.Enabled = false;
            cmdEnable.Enabled = false;

        }
        private void Reset_All()
        {
            chkAuthor.CheckState = CheckState.Unchecked;
            chkdep.CheckState = CheckState.Unchecked;
            chkNotes.CheckState = CheckState.Unchecked;
            chkTitle.CheckState = CheckState.Unchecked;

            rdoContains.Checked = false;
            rdoExact.Checked = false;
            rdoMatch.Checked = false;

            txtString.Text = "";
        }
        private void Reload_Filters()
        {

            int Last_Index = lstfilter.SelectedIndex;

            /*
            Disable_All();
            tviewFilter.SelectedNode = tviewFilter.Nodes[0];
            tviewFilter.CollapseAll();
            tviewFilter.Select();
            lstfilter.Items.Clear();
             */

            // Initialize Default Filter Arrays
            Filter_Default = new string[main.Filter_Default.Length];
            Filter_Default_Name = new string[main.Filter_Default_Name.Length];
            Filter_Default_String = new string[main.Filter_Default.Length];
            Filter_Default_Title = new bool[main.Filter_Default.Length];
            Filter_Default_Author = new bool[main.Filter_Default.Length];
            Filter_Default_Notes = new bool[main.Filter_Default.Length];
            Filter_Default_HasDep = new bool[main.Filter_Default.Length];
            Filter_Default_Mode = new int[main.Filter_Default.Length];
            Filter_Default_Enabled = new bool[main.Filter_Default.Length];

            // Import Default Filters
            for (int i = 0; i <= main.Filter_Default.Length - 1; i++)
            {
                Filter_Default[i] = main.Filter_Default[i];
                Filter_Default_Name[i] = main.Filter_Default_Name[i];
                Filter_Default_String[i] = main.Filter_Default_String[i];
                Filter_Default_Title[i] = main.Filter_Default_Title[i];
                Filter_Default_Author[i] = main.Filter_Default_Author[i];
                Filter_Default_Notes[i] = main.Filter_Default_Notes[i];
                Filter_Default_HasDep[i] = main.Filter_Default_HasDep[i];
                Filter_Default_Mode[i] = main.Filter_Default_Mode[i];
                Filter_Default_Enabled[i] = main.Filter_Default_Enabled[i];
            }

            string[] f = Directory.GetFiles(Application.StartupPath + "\\Filters");
            if (f.Length > 0)
            {
                Filter = new string[f.Length];
                Filter_Name = new string[f.Length];
                Filter_String = new string[f.Length];
                Filter_Title = new bool[f.Length];
                Filter_Author = new bool[f.Length];
                Filter_Notes = new bool[f.Length];
                Filter_HasDep = new bool[f.Length];
                Filter_Mode = new int[f.Length];
                Filter_Enabled = new bool[f.Length];

                Filter = Directory.GetFiles(Application.StartupPath + "\\Filters");
                for (int i = 0; i <= f.Length - 1; i++)
                    Filter_LoadFile(Filter[i], i);

                lstfilter.Items.Clear();

                if (tviewFilter.SelectedNode.Name == "nDefault")
                {
                    for (int i = 0; i <= Filter_Default.Length - 1; i++)
                        lstfilter.Items.Add(Filter_Default_Name[i]);
                    lstfilter.Update();

                    if (Last_Index < Filter_Default.Length)
                        lstfilter.SelectedIndex = Last_Index;
                    else
                        lstfilter.SelectedIndex = Last_Index - 1;
                }
                else if (tviewFilter.SelectedNode.Name == "nCustom")
                {
                    for (int i = 0; i <= Filter.Length - 1; i++)
                        lstfilter.Items.Add(Filter_Name[i]);
                    lstfilter.Update();
                    if (Last_Index < Filter.Length)
                        lstfilter.SelectedIndex = Last_Index;
                    else
                        lstfilter.SelectedIndex = Last_Index - 1;
                }
            }
        }

        private void tviewFilter_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lstfilter.Items.Clear();
            if (tviewFilter.SelectedNode.Name == "nDefault")
            {
                Disable_All();
                for (int i = 0; i <= Filter_Default.Length - 1; i++)
                    lstfilter.Items.Add(Filter_Default_Name[i]);
                lstfilter.Update();
            }
            else if (tviewFilter.SelectedNode.Name == "nCustom")
            {
                Enable_All();
                cmdDelete.Enabled = false;
                cmdEnable.Enabled = false;
                for (int i = 0; i <= Filter.Length - 1; i++)
                    lstfilter.Items.Add(Filter_Name[i]);
                lstfilter.Update();
            }
            else
                Disable_All();
        }
        private void lstfilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstfilter.SelectedIndex >= 0)
            {
                Reset_All();
                if (tviewFilter.SelectedNode.Name == "nDefault")
                {
                    Reset_All();
                    Disable_All();

                    if (Filter_Default_Enabled[lstfilter.SelectedIndex] == true)
                    {
                        Enable_Mode = 2;
                        cmdEnable.Text = "Disable";
                    }
                    else
                    {
                        Enable_Mode = 1;
                        cmdEnable.Text = "Enable";
                    }

                    txtString.Text = Filter_Default_String[lstfilter.SelectedIndex];

                    if (Filter_Default_Title[lstfilter.SelectedIndex] == true)
                        chkTitle.CheckState = CheckState.Checked;
                    if (Filter_Default_Author[lstfilter.SelectedIndex] == true)
                        chkAuthor.CheckState = CheckState.Checked;
                    if (Filter_Default_Notes[lstfilter.SelectedIndex] == true)
                        chkNotes.CheckState = CheckState.Checked;
                    if (Filter_Default_HasDep[lstfilter.SelectedIndex] == true)
                        chkNotes.CheckState = CheckState.Checked;

                    if (Filter_Default_Mode[lstfilter.SelectedIndex] == 1)
                    {
                        rdoExact.Checked = true;
                        rdoMatch.Checked = false;
                        rdoContains.Checked = false;
                    }
                    else if (Filter_Default_Mode[lstfilter.SelectedIndex] == 2)
                    {
                        rdoExact.Checked = false;
                        rdoMatch.Checked = true;
                        rdoContains.Checked = false;
                    }
                    else if (Filter_Default_Mode[lstfilter.SelectedIndex] == 3)
                    {
                        rdoExact.Checked = false;
                        rdoMatch.Checked = false;
                        rdoContains.Checked = true;
                    }
                }
                else if (tviewFilter.SelectedNode.Name == "nCustom")
                {
                    Reset_All();
                    Enable_All();

                    if (Filter_Enabled[lstfilter.SelectedIndex] == true)
                    {
                        Enable_Mode = 2;
                        cmdEnable.Text = "Disable";
                    }
                    else
                    {
                        Enable_Mode = 1;
                        cmdEnable.Text = "Enable";
                    }

                    txtString.Text = Filter_String[lstfilter.SelectedIndex];

                    if (Filter_Title[lstfilter.SelectedIndex] == true)
                        chkTitle.CheckState = CheckState.Checked;
                    if (Filter_Author[lstfilter.SelectedIndex] == true)
                        chkAuthor.CheckState = CheckState.Checked;
                    if (Filter_Notes[lstfilter.SelectedIndex] == true)
                        chkNotes.CheckState = CheckState.Checked;
                    if (Filter_HasDep[lstfilter.SelectedIndex] == true)
                        chkdep.CheckState = CheckState.Checked;

                    if (Filter_Mode[lstfilter.SelectedIndex] == 1)
                    {
                        rdoExact.Checked = true;
                        rdoMatch.Checked = false;
                        rdoContains.Checked = false;
                    }
                    else if (Filter_Mode[lstfilter.SelectedIndex] == 2)
                    {
                        rdoExact.Checked = false;
                        rdoMatch.Checked = true;
                        rdoContains.Checked = false;
                    }
                    else if (Filter_Mode[lstfilter.SelectedIndex] == 3)
                    {
                        rdoExact.Checked = false;
                        rdoMatch.Checked = false;
                        rdoContains.Checked = true;
                    }
                }
            }
            else
                MessageBox.Show ("is " + Convert.ToString (lstfilter.SelectedIndex));
            }

        private void cmdok_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= Filter.Length - 1; i++)
                Filter_Save();
            this.Close();
        }
        private void cmdenable_Click(object sender, EventArgs e)
        {
            if (tviewFilter.SelectedNode.Name == "nDefault")
            {
                if (Enable_Mode == 1)
                {
                    cmdEnable.Text = "Disable";
                    Filter_Default_Enabled[lstfilter.SelectedIndex] = true; 
                    Filter_Save();

                }
                else
                {
                    cmdEnable.Text = "Enable";
                    Filter_Default_Enabled[lstfilter.SelectedIndex] = false;
                    Filter_Save();
                }
            }
            else if (tviewFilter.SelectedNode.Name == "nCustom")
                if (Enable_Mode == 1)
                {
                    cmdEnable.Text = "Disable";
                    Filter_Enabled[lstfilter.SelectedIndex] = true;
                    Filter_Save();
                }
                else
                {
                    cmdEnable.Text = "Enable";
                    Filter_Enabled[lstfilter.SelectedIndex] = false;
                    Filter_Save();
                }
        }
        private void cmddelete_Click(object sender, EventArgs e)
        {
            if (tviewFilter.SelectedNode.Name == "nDefault")
            {
                bool confirm = Convert.ToBoolean(MessageBox.Show("Once delete, a filter cannot be recovered. Are you sure you want to delete the filter: " + Filter_Default_Name[lstfilter.SelectedIndex] + "?","Comfirm Delete",MessageBoxButtons.YesNo,MessageBoxIcon.Warning));
                if (confirm == true)
                {
                    File.Delete(Filter_Default[lstfilter.SelectedIndex]);
                    Reload_Filters();
                }
            }
            else if (tviewFilter.SelectedNode.Name == "nCustom")
            {
                bool confirm = Convert.ToBoolean(MessageBox.Show("Once deleted, a filter cannot be recovered. Are you sure you want to delete the filter: " + Filter_Name[lstfilter.SelectedIndex] + "?", "Comfirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning));
                if (confirm == true)
                {
                    File.Delete(Filter[lstfilter.SelectedIndex]);
                    Reload_Filters();
                }
            }

        }
        private void cmdNew_Click(object sender, EventArgs e)
        {
            frmnewfilter frm = new frmnewfilter();
            frm.Show();
            Use_Activate = true;          
        }
        private void frmfilters_Activated(object sender, EventArgs e)
        {
            if (Use_Activate == true)
            {
                Reload_Filters();
                Use_Activate = false;
            }
            
        }

        private void chkTitle_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTitle.CheckState == CheckState.Checked)
                Filter_Title[lstfilter.SelectedIndex] = true;
            else
                Filter_Title[lstfilter.SelectedIndex] = false;
        }
        private void chkAuthor_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAuthor.CheckState == CheckState.Checked)
                Filter_Author[lstfilter.SelectedIndex] = true;
            else
                Filter_Author[lstfilter.SelectedIndex] = false;
        }
        private void chkNotes_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNotes.CheckState == CheckState.Checked)
                Filter_Notes[lstfilter.SelectedIndex] = true;
            else
                Filter_Notes[lstfilter.SelectedIndex] = false;
        }
        private void chkdep_CheckedChanged(object sender, EventArgs e)
        {
            if (chkdep.CheckState == CheckState.Checked)
                Filter_HasDep[lstfilter.SelectedIndex] = true;
            else
                Filter_HasDep[lstfilter.SelectedIndex] = false;
        }

        private void txtString_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Filter_String[lstfilter.SelectedIndex] = txtString.Text;
        }

        private void rdoExact_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoExact.Checked == true)
                Filter_Mode[lstfilter.SelectedIndex] = 1;
        }
        private void rdoMatch_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoMatch.Checked == true)
                Filter_Mode[lstfilter.SelectedIndex] = 2;
        }
        private void rdoContains_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoContains.Checked == true)
                Filter_Mode[lstfilter.SelectedIndex] = 3;
        }
        }
    }
